package com.servlet;

import com.Mapper.UserMapper;
import com.bean.Parked;
import com.bean.Rule;
import com.bean.VipUser;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/DeleteParkingServlet")
public class DeleteParkingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String number = request.getParameter("number");
        System.out.println(number);
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateParking(number);
        Parked order = mapper.selectParked(number);
        List<VipUser> vipUsers = mapper.selectVipUser(number);
        Rule rule = mapper.selectRule();

        Integer fee;
        Integer day;
        Integer a;
        Integer b;
        Integer time = Integer.valueOf(order.getAllTime());
        if (vipUsers.size()==0) {
            if (time==1) {
                fee=0;
            } else {
                day = time / 24;
                a = time % 24;
                b = a % rule.getCostTime() == 0 ? a/rule.getCostTime():a/rule.getCostTime()+1;
                fee = day * rule.getDayCost()+b*rule.getPerCost();
            }
        } else {
            fee = 0;
        }
        mapper.updateParked(number,fee);
        mapper.deleteParking(number);
        sqlSession.close();
        out.print(fee+","+vipUsers.size());
//        mapper.claCost(parked);
//        System.out.println(parked.getFee()+parked.getKind());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }
}
